#!/bin/bash

# include password and personal settings 
# (this should be the only line in this file you need to alter to use the script)
. /Volumes/mandragon/scripts/deploySettings

function checkIn {
	svn ci $basePath --username $svnUser --password $svnPassword --editor-cmd $svnEditor
}

function checkOut {
    mkdir $checkOutPath
    svn co $svnRepoUrl $checkOutPath --username $svnUser --password $svnPassword   
}

function cleanUp {
    rm -Rf $checkOutPath   
}

function renamePhpFiles {
	 find $checkOutPath -type f -name *.php -exec mv {} {}5 \;
}

function removeScriptFolder {
	 rm -Rf $checkOutPath/$scriptFolder
}

function removeSvnFolders {
	 find $checkOutPath -type d -name .svn -exec rm -Rf {} \;
}

function removeTestFolder {
	 rm -Rf $checkOutPath/$testFolder
}

function reconfigureConfigFile {
	 perl -i.bak -p -e"s/(\\\$CONFIG\[['\"]db_user['\"]]\s*?=\s*?['\"])(.*?)(['\"])/\$1$dbUser\$3/" $checkOutPath/$configFile
	 perl -i.bak1 -p -e"s/(\\\$CONFIG\[['\"]db_pwd['\"]]\s*?=\s*?['\"])(.*?)(['\"])/\$1$dbPassword\$3/" $checkOutPath/$configFile
	 perl -i.bak2 -p -e"s/(\\\$CONFIG\[['\"]db_host['\"]]\s*?=\s*?['\"])(.*?)(['\"])/\$1$dbHost\$3/" $checkOutPath/$configFile
	 perl -i.bak2 -p -e"s/(\\\$CONFIG\[['\"]db_name['\"]]\s*?=\s*?['\"])(.*?)(['\"])/\$1$dbName\$3/" $checkOutPath/$configFile
}

function adaptPhpFilesInHtaccess {
	 perl -i.bak -p -e"s/\.php/.php5/g" $checkOutPath/$htaccessFile
}

function ftpToServer {
	 ncftpput -R -u $ftpUser -p $ftpPassword -m $ftpHost $ftpHostFolder $checkOutPath/*
}

updateResult=$(svn update $basePath --username $svnUser --password $svnPassword)

lintResult=$(find $basePath -name *.inc -exec php -l {} \;)

grepLintResult=$(echo $lintResult | grep -n "Parse error:")

if [ -n "$grepLintResult"]; then
	checkIn
#   everything except for checkin has been temporarily disabled.
#	checkOut
#	removeScriptFolder
#	#run test suite and verify results
#	removeSvnFolders
#	removeTestFolder
#	renamePhpFiles
#	reconfigureConfigFile
#	adaptPhpFilesInHtaccess
#	ftpToServer
#	cleanUp
else
	echo "Please correct the following errors first: $lintResult"
	exit
fi
